<template>
  <div>
    <div class="page_logo_top">
      <span class="span_title">EHR 后台管理系统</span>
      <div class="right_btns">
        <span>你好，{{userName}}</span>
        <span><NuxtLink to="/setting">个人设置</NuxtLink></span>
        <span><NuxtLink to="/about">关于</NuxtLink></span>
        <a-button type="link" @click="logoutHandle">退出系统</a-button>
      </div>
    </div>
    <a-menu mode="horizontal" theme="dark" :default-selected-keys="activeMenu" @click="menuHandle">
      <template v-for="menu in menus">
        <a-sub-menu v-if="menu.children&&menu.children.length" :key="menu.path">
          <template #title>{{menu.name}}</template>
          <a-menu-item v-for="child in menu.children" :key="child.path">{{child.name}}</a-menu-item>
        </a-sub-menu>
        <template v-else>
          <a-menu-item :key="menu.path">{{menu.name}}</a-menu-item>
        </template>
      </template>
    </a-menu>
  </div>
</template>

<script>
export default {
  name: 'AppHeader',
  computed: {
    userName: function() {
      return this.$store.state.user.name
    },
    menus: function() {
      return this.$store.state.menu.menus
    },
    activeMenu: function() {
      return [this.$route.path]
    }
  },
  methods: {
    logoutHandle() {
      this.$confirm({
        title: '提示',
        content: '确定退出系统？',
        cancelText: '取消',
        okText: '确定',
        onOk: () => {
          this.$axios.post('/api/signout').then(({code, msg}) => {
            if(code === 200) {
              this.$cookies.remove('c_user')
              this.$cookies.remove('c_menu')
              this.$store.commit('user/M_UPDATE_USER', {err: -1, token: '', msg: '未登录'})
              this.$message.success(msg)
              this.$router.push('/login')
            }
          })
        }
      })
    },
    menuHandle(n) {
      this.$router.push(n.key)
    }
  }
}
</script>

<style lang="less">
  .page_logo_top{
    height: 58px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 24px;
  }
  .span_title{
    font-size: 17px;
    color: @primary-color;
    font-weight: bold;
  }
  .right_btns{
    & > span{
      margin-left: 12px;
      position: relative;
      &::after{
        content: '';
        position: absolute;
        width: 1px;
        height: 12px;
        background-color: @primary-color;
        top: 4px;
        right: -10px;
      }
    }
  }
  </style>